package com.yckj.activechicken.business.goods.controller;

import com.alibaba.fastjson.JSON;
import com.yckj.activechicken.business.goods.model.GoodsAttribute;
import com.yckj.activechicken.business.goods.model.GoodsInfo;
import com.yckj.activechicken.business.goods.model.po.RemoveFile;
import com.yckj.activechicken.business.goods.service.IGoodsService;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.goods.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【货品管理请求控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/7 20:39
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;

    /**
     * 添加货品信息
     * @param goodsInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('goods:addGoods')")
    @PostMapping("/addGoods")
    public AjaxResult addGoods(GoodsInfo goodsInfo) {
        String username = SecurityUtils.getUsername();
        goodsInfo.setCreateBy(username);
        Date newDate = new Date();
        goodsInfo.setCreateTime(newDate);
        goodsInfo.setUpdateBy(username);
        goodsInfo.setUpdateTime(newDate);

        List<GoodsAttribute> goodsAttributeList =
                JSON.parseArray(goodsInfo.getGoodsAttrListJson(),GoodsAttribute.class);
        goodsInfo.setGoodsAttributeList(goodsAttributeList);

        Map<String,String> outMap = goodsService.addGoods(goodsInfo);
        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    /**
     * 分页查询货品列表信息
     * @param goodsInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsList')")
    @GetMapping("/listGoods")
    public TableDataInfo listGoods(GoodsInfo goodsInfo) {
        startPage();
        List<GoodsInfo> list = goodsService.listGoods(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 修改货品信息
     * @param goodsInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('goods:modifyGoods')")
    @PostMapping("/modifyGoods")
    public AjaxResult modifyGoods(GoodsInfo goodsInfo){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        goodsInfo.setUpdateBy(username);
        goodsInfo.setUpdateTime(newDate);

        List<GoodsAttribute> goodsAttributeList =
                JSON.parseArray(goodsInfo.getGoodsAttrListJson(),GoodsAttribute.class);
        goodsInfo.setGoodsAttributeList(goodsAttributeList);

        List<RemoveFile> removeFileList =
                JSON.parseArray(goodsInfo.getRemoveFileListJson(),RemoveFile.class);
        goodsInfo.setRemoveFileList(removeFileList);

        Map<String,String> outMap = goodsService.modifyGoods(goodsInfo);
        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    /**
     * 删除货品信息
     * @param goodsInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('goods:delGoods')")
    @PostMapping("/delGoods")
    public AjaxResult delGoods(@RequestBody GoodsInfo goodsInfo){
        GoodsInfo delGoodsInfo = new GoodsInfo();
        delGoodsInfo.setGoodsId(goodsInfo.getGoodsId());
        //将删除标识设置为1
        delGoodsInfo.setDelFlag("1");
        delGoodsInfo.setUpdateBy(SecurityUtils.getUsername());
        delGoodsInfo.setUpdateTime(new Date());

        return toAjax(goodsService.delGoods(delGoodsInfo));
    }
}
